/*     */ package com.zimbra.cs.pop3;
/*     */ 
/*     */ import com.google.common.base.Strings;
/*     */ import com.google.common.collect.ImmutableSet;
/*     */ import com.google.common.io.Closeables;
/*     */ import com.zimbra.common.account.ZAttrProvisioning.PrefPop3DeleteOption;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.DateUtil;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.index.MessageHit;
/*     */ import com.zimbra.cs.index.SortBy;
/*     */ import com.zimbra.cs.index.ZimbraHit;
/*     */ import com.zimbra.cs.index.ZimbraQueryResults;
/*     */ import com.zimbra.cs.mailbox.Flag;
/*     */ import com.zimbra.cs.mailbox.Flag.FlagInfo;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxIndex;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collections;
/*     */ import java.util.Date;
/*     */ import java.util.EnumSet;
/*     */ import java.util.List;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ final class Pop3Mailbox
/*     */ {
/*  50 */   private static final Set<MailItem.Type> POP3_TYPES = EnumSet.of(MailItem.Type.MESSAGE);
/*     */   
/*     */   private final int id;
/*     */   private int numDeleted;
/*     */   private long totalSize;
/*  55 */   private long deletedSize = 0L;
/*     */   
/*     */   private final List<Pop3Message> messages;
/*     */   
/*     */   private final OperationContext opContext;
/*     */   private final ZAttrProvisioning.PrefPop3DeleteOption deleteOption;
/*     */   
/*     */   Pop3Mailbox(Mailbox mbox, Account acct, String query)
/*     */     throws ServiceException
/*     */   {
/*  65 */     this.id = mbox.getId();
/*  66 */     this.numDeleted = 0;
/*  67 */     this.opContext = new OperationContext(acct);
/*  68 */     this.deleteOption = acct.getPrefPop3DeleteOption();
/*     */     
/*  70 */     if (Strings.isNullOrEmpty(query)) {
/*  71 */       Set<Integer> folderIds = acct.isPrefPop3IncludeSpam() ? ImmutableSet.of(Integer.valueOf(2), Integer.valueOf(4)) : Collections.singleton(Integer.valueOf(2));
/*     */       
/*     */ 
/*  74 */       String dateConstraint = acct.getAttr("zimbraPrefPop3DownloadSince");
/*  75 */       Date popSince = dateConstraint == null ? null : DateUtil.parseGeneralizedTime(dateConstraint);
/*  76 */       this.messages = mbox.openPop3Folder(this.opContext, folderIds, popSince);
/*  77 */       for (Pop3Message p3m : this.messages) {
/*  78 */         this.totalSize += p3m.getSize();
/*     */       }
/*     */     } else {
/*  81 */       ZimbraQueryResults results = null;
/*  82 */       this.messages = new ArrayList(500);
/*     */       try {
/*  84 */         results = mbox.index.search(this.opContext, query, POP3_TYPES, SortBy.DATE_DESC, 500);
/*     */         
/*  86 */         while (results.hasNext()) {
/*  87 */           ZimbraHit hit = results.getNext();
/*  88 */           if ((hit instanceof MessageHit)) {
/*  89 */             MessageHit mh = (MessageHit)hit;
/*  90 */             Message msg = mh.getMessage();
/*  91 */             if (!msg.isTagged(Flag.FlagInfo.POPPED)) {
/*  92 */               this.totalSize += msg.getSize();
/*  93 */               this.messages.add(new Pop3Message(msg));
/*     */             }
/*     */           }
/*     */         }
/*     */       } finally {
/*  98 */         Closeables.closeQuietly(results);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   int getId()
/*     */   {
/* 107 */     return this.id;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   long getSize()
/*     */   {
/* 114 */     return this.totalSize - this.deletedSize;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   int getNumMessages()
/*     */   {
/* 121 */     return this.messages.size() - this.numDeleted;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   int getTotalNumMessages()
/*     */   {
/* 128 */     return this.messages.size();
/*     */   }
/*     */   
/*     */ 
/*     */   Pop3Message getMsg(int index)
/*     */     throws Pop3CmdException
/*     */   {
/* 135 */     if ((index < 0) || (index >= this.messages.size())) {
/* 136 */       throw new Pop3CmdException("invalid message");
/*     */     }
/* 138 */     Pop3Message p3m = (Pop3Message)this.messages.get(index);
/* 139 */     return p3m;
/*     */   }
/*     */   
/*     */   private int parseInt(String s, String message) throws Pop3CmdException {
/*     */     try {
/* 144 */       return Integer.parseInt(s);
/*     */     } catch (NumberFormatException e) {
/* 146 */       throw new Pop3CmdException(message);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   Pop3Message getPop3Msg(String msg)
/*     */     throws Pop3CmdException
/*     */   {
/* 154 */     int index = parseInt(msg, "unable to parse msg");
/* 155 */     Pop3Message p3m = getMsg(index - 1);
/* 156 */     if (p3m.isDeleted())
/* 157 */       throw new Pop3CmdException("message is deleted");
/* 158 */     return p3m;
/*     */   }
/*     */   
/*     */ 
/*     */   Message getMessage(String msg)
/*     */     throws Pop3CmdException, ServiceException
/*     */   {
/* 165 */     Pop3Message p3m = getPop3Msg(msg);
/* 166 */     Mailbox mbox = MailboxManager.getInstance().getMailboxById(this.id);
/* 167 */     return mbox.getMessageById(this.opContext, p3m.getId());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   void delete(Pop3Message p3m)
/*     */   {
/* 174 */     if (!p3m.isDeleted()) {
/* 175 */       p3m.setDeleted(true);
/* 176 */       this.numDeleted += 1;
/* 177 */       this.deletedSize += p3m.getSize();
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   int undeleteMarked()
/*     */   {
/* 185 */     int count = 0;
/* 186 */     for (int i = 0; i < this.messages.size(); i++) {
/* 187 */       Pop3Message p3m = (Pop3Message)this.messages.get(i);
/* 188 */       if (p3m.isDeleted()) {
/* 189 */         this.numDeleted -= 1;
/* 190 */         this.deletedSize -= p3m.getSize();
/* 191 */         p3m.setDeleted(false);
/* 192 */         count++;
/*     */       }
/*     */     }
/* 195 */     return count;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   int expungeDeletes()
/*     */     throws ServiceException, Pop3CmdException
/*     */   {
/* 205 */     Mailbox mbox = MailboxManager.getInstance().getMailboxById(this.id);
/* 206 */     int count = 0;
/* 207 */     int failed = 0;
/* 208 */     for (Pop3Message p3msg : this.messages) {
/* 209 */       if (p3msg.isDeleted()) {
/*     */         try {
/* 211 */           switch (this.deleteOption) {
/*     */           case keep: 
/* 213 */             updateFlags(mbox, p3msg.getId(), true, false);
/* 214 */             break;
/*     */           case read: 
/* 216 */             updateFlags(mbox, p3msg.getId(), true, true);
/* 217 */             break;
/*     */           case trash: 
/* 219 */             updateFlags(mbox, p3msg.getId(), true, true);
/* 220 */             mbox.move(this.opContext, p3msg.getId(), MailItem.Type.MESSAGE, 3);
/* 221 */             break;
/*     */           case delete: 
/* 223 */             mbox.delete(this.opContext, p3msg.getId(), MailItem.Type.MESSAGE);
/* 224 */             break;
/*     */           default: 
/* 226 */             if (!$assertionsDisabled) throw new AssertionError(this.deleteOption);
/*     */             break; }
/* 228 */           count++;
/*     */         } catch (ServiceException e) {
/* 230 */           ZimbraLog.pop.warn("Failed to expunge delete", e);
/* 231 */           failed++;
/*     */         }
/* 233 */         this.numDeleted -= 1;
/* 234 */         this.deletedSize -= p3msg.getSize();
/* 235 */       } else if (p3msg.isRetrieved()) {
/*     */         try {
/* 237 */           switch (this.deleteOption) {
/*     */           case read: 
/* 239 */             updateFlags(mbox, p3msg.getId(), false, true);
/*     */           }
/*     */         }
/*     */         catch (ServiceException e) {
/* 243 */           ZimbraLog.pop.warn("Failed to update flags", e);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 248 */     if (count > 0) {
/*     */       try {
/* 250 */         mbox.resetRecentMessageCount(this.opContext);
/*     */       } catch (ServiceException e) {
/* 252 */         ZimbraLog.pop.info("error resetting mailbox recent message count", e);
/*     */       }
/*     */     }
/* 255 */     if (failed > 0) {
/* 256 */       throw new Pop3CmdException("deleted " + count + "/" + (count + failed) + " message(s)");
/*     */     }
/* 258 */     return count;
/*     */   }
/*     */   
/*     */   private void updateFlags(Mailbox mbox, int msgId, boolean poped, boolean read) throws ServiceException {
/* 262 */     Message message = mbox.getMessageById(this.opContext, msgId);
/* 263 */     int flags = message.getFlagBitmask();
/* 264 */     if (poped) {
/* 265 */       flags |= Flag.BITMASK_POPPED;
/*     */     }
/* 267 */     if (read) {
/* 268 */       flags &= (Flag.BITMASK_UNREAD ^ 0xFFFFFFFF);
/*     */     }
/* 270 */     mbox.setTags(this.opContext, msgId, MailItem.Type.MESSAGE, flags, MailItem.TAG_UNCHANGED);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/pop3/Pop3Mailbox.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */